package com.andy.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

import java.security.Key;

/**
 * 令牌桶 限流规则配置
 */
@Configuration
public class KeyResolverConfiguration {
    /**
     * 根据路径限流
     */
    @Bean
    public KeyResolver pathKeyResolver(){
        // JDK 1.8
        return exchange -> Mono.just(exchange.getRequest().getURI().getPath());
    }

    /**
     * 根据参数限流
     */
//    @Bean
    public KeyResolver paramKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("userId"));
    }

    /**
     * 根据IP限流
     */
//    @Bean
    public KeyResolver iPKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }
}
